package com.example.demo01.controller;

import com.example.demo01.entity.UserEntity;
import com.example.demo01.jpa.UserJPA;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Description:
 * @ProjectName: demo01
 * @Author: houzq
 * @Date: 2020-04-27
 * @Version: V1.0
 */
@RestController
@RequestMapping(value = "/user")
public class UserController {

    @Autowired
    private UserJPA userJPA;

    /***
     *@Description 查询用户列表的方法
     *@Author hzq
     *@Date 2020/4/27
     *@Params []
     *@Results java.util.List<com.example.demo01.entity.User>
     */
    @RequestMapping(value = "/userList", method = RequestMethod.GET)
    public List<UserEntity> getUserList(){
        return userJPA.findAll();
    }

    /***
     *@Description: 增加或者更新用户的方法
     *@Author : hzq
     *@Date   : 2020/4/27
     *@Params : [user]
     *@Results: com.example.demo01.entity.User
     */
    @RequestMapping(value = "/addUser", method = RequestMethod.GET)
    public UserEntity addUser(UserEntity userEntity){
        return userJPA.save(userEntity);
    }

    @RequestMapping(value = "/delUser", method = RequestMethod.GET)
    public List<UserEntity> removeUser(Integer id){
        userJPA.deleteById(id);
        return userJPA.findAll();
    }
}
